const chalk = require('chalk');
// const msgPath = process.env.GIT_PARAMS // 此为 undefined 拿不到 msg 信息
// process.env 对象中没有任何 commit 信息，process.argv 中本来也没有
// pre-commit 里的 $1 必填，这里才能拿到本次的 CommitMsg
const msgPath = process.env.GIT_PARAMS || process.env.HUSKY_GIT_PARAMS || process.argv[2]; //使用环境变量获取到commit message
// console.log(process.argv,'process.argv');
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim();
console.log(); // 空行，下面打印提示信息
console.log(chalk.bgBlueBright.white('Commit Msg: '), msg);
console.log(); // 打印提示信息后空行

const commitRE = /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/;

if (!commitRE.test(msg)) {
  // 下面是一些错误日志提醒
  console.error(
    `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`提交日志不符合规范`)}\n\n${chalk.red(
      `  合法的提交日志格式如下(emoji 和 模块可选填)：\n\n`
    )}    
    ${chalk.green(`[<emoji>] [revert: ?]<type>[(scope)?]: <message>\n`)}
    ${chalk.green(`💥 feat(模块): 添加了个很棒的功能`)}
    ${chalk.green(`🐛 fix(模块): 修复了一些 bug`)}
    ${chalk.green(`📝 docs(模块): 更新了一下文档`)}
    ${chalk.green(`🌷 UI(模块): 修改了一下样式`)}
    ${chalk.green(`🏰 chore(模块): 对脚手架做了些更改`)}
    ${chalk.green(`🌐 locale(模块): 为国际化做了微小的贡献\n`)}
    ${chalk.green(`其他提交类型: refactor, perf, workflow, build, CI, typos, tests, types, wip, release, dep\n`)}
    ${chalk.red(`See https://github.com/vuejs/core/blob/main/.github/commit-convention.md\n`)}`
  );
  process.exit(1); // 如果不规范退出运行进程
}

// const childProcess = require('child_process');

// // 提交记录
// const commitStages = childProcess.execSync('git diff --name-status HEAD~3', {
//   encoding: 'utf8',
// });
// console.log('提交记录：', commitStages);
